package pers.amos.eshop.advance.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.amos.eshop.advance.product.common.DataTypeConstants;
import pers.amos.eshop.advance.product.common.EventTypeConstants;
import pers.amos.eshop.advance.product.mapper.ProductIntroMapper;
import pers.amos.eshop.advance.product.model.ProductIntro;
import pers.amos.eshop.advance.product.mq.RabbitMQSender;
import pers.amos.eshop.advance.product.service.ProductIntroService;


@Service
public class ProductIntroServiceImpl implements ProductIntroService {

    @Autowired
    private ProductIntroMapper productIntroMapper;

    @Autowired
    private RabbitMQSender rabbitMQSender;

    @Override
    public void add(ProductIntro productIntro, String operationType) {
        productIntroMapper.add(productIntro);

        String sendMessage = RabbitMQSender.buildProductDimData(EventTypeConstants.ADD_EVENT_TYPE,
                DataTypeConstants.PRODUCT_INTRO_DATA_TYPE, productIntro.getId(), productIntro.getProductId());

        String queue = rabbitMQSender.decideSendMegQueue(operationType);

        rabbitMQSender.send(queue, sendMessage);
    }

    @Override
    public void update(ProductIntro productIntro, String operationType) {
        productIntroMapper.update(productIntro);

        String sendMessage = RabbitMQSender.buildProductDimData(EventTypeConstants.UPDATE_EVENT_TYPE,
                DataTypeConstants.PRODUCT_INTRO_DATA_TYPE, productIntro.getId(), productIntro.getProductId());

        String queue = rabbitMQSender.decideSendMegQueue(operationType);

        rabbitMQSender.send(queue, sendMessage);
    }

    @Override
    public void delete(Long id, String operationType) {
        productIntroMapper.delete(id);

        String sendMessage = RabbitMQSender.buildSendData(EventTypeConstants.DELETE_EVENT_TYPE,
                DataTypeConstants.PRODUCT_INTRO_DATA_TYPE, id);

        String queue = rabbitMQSender.decideSendMegQueue(operationType);

        rabbitMQSender.send(queue, sendMessage);
    }

    @Override
    public ProductIntro findById(Long id) {
        return productIntroMapper.findById(id);
    }

}
